package com.jamal2362.apktool.util.sai;

import android.app.Service;
import android.content.Intent;
import android.content.pm.PackageInstaller;
import android.os.IBinder;
import androidx.annotation.Nullable;
import com.jamal2362.apktool.MainActivity;

public class InstallService extends Service {
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        int status = intent.getIntExtra(PackageInstaller.EXTRA_STATUS, -999);
		String message = intent.getStringExtra(PackageInstaller.EXTRA_STATUS_MESSAGE);
		final String pn = intent.getStringExtra("packageName");
        switch (status) {
            case PackageInstaller.STATUS_PENDING_USER_ACTION:
                Intent confirmationIntent = intent.getParcelableExtra(Intent.EXTRA_INTENT);
                confirmationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                try {
                    startActivity(confirmationIntent);
                } catch (Exception e) {
					e.printStackTrace();
                }
                break;

//            case PackageInstaller.STATUS_SUCCESS:
//				Intent cin = new Intent(MainActivity.ACTION_FILTER);
//				cin.putExtra("packageName", pn);
//				sendOrderedBroadcast(cin, null);
//                break;

            default:
//				Toast.makeText(getApplicationContext(), getApplicationContext().getString(R.string.install_fail, status), Toast.LENGTH_SHORT).show();
				Intent cin = new Intent(MainActivity.ACTION_FILTER);
				cin.putExtra("packageName", pn);
				cin.putExtra("success", (status == PackageInstaller.STATUS_SUCCESS));
				cin.putExtra("status", status);
				cin.putExtra("message", message);
				sendOrderedBroadcast(cin, null);
                break;
        }
        stopSelf();
        return START_NOT_STICKY;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
